package edu.lagou.service.impl;

import edu.lagou.dao.AuthcodeDao;
import edu.lagou.pojo.Authcode;
import edu.lagou.service.AuthcodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.Calendar;
import java.util.NoSuchElementException;
import java.util.Random;

@Service
public class AuthcodeServiceImpl implements AuthcodeService {

    @Autowired
    private AuthcodeDao authcodeDao;

    @Override
    public void save(Authcode authcode) {
        authcodeDao.save(authcode);
    }

    @Override
    public String createCode(String email) {
        String code = null;

        Timestamp cTime = new Timestamp(Calendar.getInstance().getTimeInMillis());
        Calendar eCal = Calendar.getInstance();
        eCal.add(Calendar.MINUTE, 6);
        Timestamp eTime = new Timestamp(eCal.getTimeInMillis());

        Authcode authcode = null;
        try {
            authcode = findByEmail(email);
            code = makeCode();
            authcode.setCode(code);
            System.out.println(">>> expire time: " + eTime);
            authcode.setExpiretime(eTime);
            System.out.println(">> expire time set: " + authcode.getExpiretime());
        } catch (NoSuchElementException e) {
            authcode = new Authcode();
            authcode.setEmail(email);
            code = makeCode();
            authcode.setCode(code);
            authcode.setCreatetime(cTime);
            authcode.setExpiretime(eTime);
        } finally {
            save(authcode);
        }


        return code;
    }

    @Override
    public String verifyCode(String email, String code) {
        String result = "1";

        Authcode authcode = findByEmail(email);
        if (authcode != null) {
            Timestamp nTime = new Timestamp(Calendar.getInstance().getTimeInMillis());
            if (authcode.getExpiretime().before(nTime)) {
                result = "2";
            } else if (authcode.getCode().equals(code)) {
                result = "0";
            }
        }
        return result;
    }

    @Override
    public void savePassword(String email, String password) {
        Authcode authcode = findByEmail(email);
        if (authcode != null) {
            authcode.setPassword(password);
            save(authcode);
        }
    }

    @Override
    public boolean login(String email, String password) {
        Authcode authcode = findByEmail(email);
        boolean result = false;
        if (authcode != null && authcode.getPassword().equals(password)) {
            result = true;
        }

        return result;
    }

    @Override
    public Authcode findByEmail(String email) {
        Authcode authcode = new Authcode();
        authcode.setEmail(email);
        Example<Authcode> example = Example.of(authcode);
        return authcodeDao.findOne(example).get();
    }

    protected String makeCode() {
        Random random = new Random();
        String result = "";
        for (int i = 0; i < 6; i++) {
            result += random.nextInt(10);
        }

        return result;
    }
}
